package hotel.quartz.oversea.job.switchs;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.quartz.UrlSwitchsEnums;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.Result;
import hotel.quartz.oversea.config.service.RedisService;
import hotel.quartz.oversea.job.base.BaseJob;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: lmf
 * @Date: 2021/04/21/10:03
 * @Description:
 */
@Slf4j
public class SwitchPushRoomToSwitchJob implements BaseJob {

    private String timestamp = ""+String.valueOf(System.currentTimeMillis());

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisService redisService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("switch push room to  switch Job {} >>>>>>>> 触发携程接口处理缓存变化数据定时器 执行时间: {}", timestamp, DateUtil.getCurrTime());
        Long size=redisTemplate.opsForList().size(CommonConstants.SWITCH_CALLBACK_PUSH_DATA_QUEUE);
        if(size==0){
            log.info("============switch push room to  switch Job 队列长度为0 跳过======");
        }else{
            request();
        }
        log.info("switch push room to  switch Job {} >>>>>>>> 触发携程接口处理缓存变化数据定时器  结束时间: {}", timestamp, DateUtil.getCurrTime());

    }
    /***
    * @Author: lmf
    * @Date: 2021/4/21
     * 调用携程推送数据接口
    */
    private void request() {
        String callbackHost = CommonUrlConstants.CALLBACK_HOST;
        log.info("switch push room to  switch Job {} 触发携程接口处理缓存变化数据定时器 进入方法 request",timestamp);
        //发起请求
        Map params = new HashMap();
        Result response;
        String checkGetChangeGoodsByOneHotelUrl = callbackHost+ UrlSwitchsEnums.goodsPrice.SWITCH_PUSH_ROOM_TO_SWITCH.getValue();
        response= JobRestTemplateUtils.post(checkGetChangeGoodsByOneHotelUrl, JSON.toJSONString(params),restTemplate);
        if(response.getCode()== CommonConstants.SUCCESS){

        }else{
            //还原
            log.error("switch push room to  switch Job {} 触发携程接口处理缓存变化数据定时器进入方法 request 返回失败：{},{}",timestamp, JSON.toJSONString(response), JSON.toJSONString(params));
        }

    }
}
